package com.example.home.controller;

import com.example.home.Dao.CateRepository;
import com.example.home.Entities.Cate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("cate")
public class CateController {

    @Autowired
    CateRepository cateRepository;

    @GetMapping
    public String outStudentForm1(Model model){

        List<Cate> cateO= cateRepository.findAll();
        model.addAttribute("cateOne", cateO);
        return "cate/list-cate";
    }


    @GetMapping("/new")
    public String outStudentForm(Model model){

        Cate cate = new Cate();
        model.addAttribute("Cate", cate);
        return "cate/Cate";
    }

    @PostMapping("/save")
    public String CreateStudent(Model model, Cate cate){

        //处理保存数据库的行为
        cateRepository.save(cate);
        //使用定位防止重复提交
        return "redirect:/cate/new";

    }

}
